﻿using System;
using System.Collections.Generic;
using System.Text;
using MediaCenterCommander.RemoteInterfaces;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter;


namespace MediaCenterCommander.Code
{
    public class MediaCenterController : MarshalByRefObject, IMediaCenterController
    {
        private AddInHost host;
        private MediaCenterActionQueue actionQueue;

        public MediaCenterController(AddInHost host, MediaCenterActionQueue actionQueue)
        {
            this.host = host;
            this.actionQueue = actionQueue;
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }

        public void SwitchToLiveTV()
        {            
            this.host.MediaCenterEnvironment.NavigateToPage(PageId.LiveTV, null);
        }

        public void WatchTVChannel(int channel)
        {            
            this.host.MediaCenterEnvironment.PlayMedia(MediaType.TV, channel, false);                
        }

        public void StartSlideshow(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                this.host.MediaCenterEnvironment.NavigateToPage(PageId.Slideshow, path);
            }
        }

        public string GetMediaCenterEnvironmentVersion()
        {
            return this.host.MediaCenterEnvironment.Version.ToString();
        }


        public void Play()
        {
            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                mediaExperience.Transport.PlayRate = PlayRateValue.Play;
            }
        }

        public void Pause()
        {
            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                mediaExperience.Transport.PlayRate = PlayRateValue.Pause;
            }
        }

        public void Stop()
        {
            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                mediaExperience.Transport.PlayRate = PlayRateValue.Stop;
            }
        }

        public void SkipForward()
        {
            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                mediaExperience.Transport.SkipForward();
            }
        }

        public void SkipBack()
        {
            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                mediaExperience.Transport.SkipBack();
            }
        }

        public string GetMediaMetadata()
        {
            StringBuilder sb = new StringBuilder();

            var mediaExperience = this.host.MediaCenterEnvironment.MediaExperience;
            if (mediaExperience != null)
            {
                var mediaMetadata = mediaExperience.MediaMetadata;
                if (mediaMetadata != null)
                {
                    foreach (var pair in mediaMetadata)
                    {
                        sb.Append(pair.Key);
                        sb.Append(": ");
                        sb.Append(pair.Value != null ? pair.Value.ToString() : null);

                        sb.AppendLine();
                    }
                }
            }

            return sb.ToString();
        }
    }

    public static class PlayRateValue
    {
        public const float Stop = 0;
        public const float Pause = 1;
        public const float Play = 2;
    }
}
